﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NhibernateClassBuilder;
using NHibernate;

namespace CreaMotion.Connectors
{
    public class SqlliteConnector : CreaMotion.Interfaces.IDBConnector
    {

        public Interfaces.Database Connect()
        {
            Interfaces.Database ReturnData = new Interfaces.Database();

            ISession MySession = ConnectorManager.CurrentNHibernateSession;

            //Get Tables
            ISQLQuery TableQuery = MySession.CreateSQLQuery("SELECT * FROM sqlite_master");
            TableQuery.AddEntity(typeof(SQLiteTables));
            IList<SQLiteTables> Tables = TableQuery.List<SQLiteTables>();

            //Fill Tables
            foreach (SQLiteTables Table in Tables)
            {
                List<Interfaces.Field> tmpFields = new List<Interfaces.Field>();

                //Get Columns
                ISQLQuery FieldQuery = MySession.CreateSQLQuery(String.Format("pragma table_info({0});",
                                                                              Table.name));
                FieldQuery.AddEntity(typeof(SQLiteColumns));
                IList<SQLiteColumns> Fields = FieldQuery.List<SQLiteColumns>();

                //Fill Fields
                foreach (SQLiteColumns Field in Fields)
                {
                    tmpFields.Add(new Interfaces.Field()
                                  {
                                    FieldName = Field.name,
                                    IsPrimaryKey = Field.pk,
                                    NotNull = Field.notnull,
                                    FieldType_db = Field.type,
                                    FieldType_net = GetDbType(Field.type),
                                  });
                }

                ReturnData.Tables.Add(new Interfaces.Table()
                                      {
                                          TableName = Table.name,
                                          Fields = tmpFields
                                      });
            }

            return ReturnData;
        }

        #region GetDbType

        public Type GetDbType(string DbTypeName)
        {

            switch (DbTypeName)
            {
                case "NUMERIC":
                case "INTEGER (strict)":
                case "INTEGER":
                    return typeof(int);
                case "DOUBLE":
                    return typeof(double);
                case "BOOL":
                    return typeof(bool);
                case "FLOAT":
                    return typeof(float);
                case "REAL":
                case "REAL (strict)":
                    return typeof(decimal);
                case "CHAR" :
                    return typeof(char);
                case "TEXT":
                case "TEXT (strict)":
                case "VARCHAR":
                    return typeof(string);
                case "BLOB":
                    return typeof(byte[]);
                case "DATETIME":
                    return typeof(DateTime);
            }
            
            return null;
        }

        #endregion

        #region About
        public Interfaces.About AboutConnector()
        {
            return new Interfaces.About()
            {
                Developer = "CreaMotion",
                Version = "1.0",
                Description = "Developed by CreaMotion",
                SupportedDatabaseSystem = "SQLite"
            };
        }
        #endregion
    }
}
